package com.example.outdoor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;

import android.app.Activity;
//import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Export extends Activity implements OnClickListener{
	 SQLiteDatabase db;
	 Button btnByNet,btnGoBackExportTables;
	  @Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.export);
	        try{
	            //opening database ( here database is "StudentDB" )
	            db=openOrCreateDatabase("OutdoorAppDB.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
	        }catch(SQLException e)
	        {
	            Log.d("Error","Error while Opening Database");
	            e.printStackTrace();
	        }
	        
	        
	        //currentDir = new File("");
	       // fill(currentDir);
	       // onFileClick();
	   
	 btnByNet=(Button)findViewById(R.id.btnByInternet);
	 btnGoBackExportTables=(Button)findViewById(R.id.btnGoBackExportTables);
	 btnByNet.setOnClickListener(this);
	 btnGoBackExportTables.setOnClickListener(this);
	
	 }
	  @Override
		public void onClick(View v) {
		  switch(v.getId())
			{
				case R.id.btnByInternet:
					 try {
						 
						 FileWriter fWriter;
						
						     fWriter = new FileWriter("/sdcard/ExportBillbaordData.txt");
						    
						
							
							 Cursor c=db.rawQuery("SELECT * FROM tblBillboardOutput",null);
							
							  c.moveToFirst();
							  String filetext="";
					            while(!c.isAfterLast())
					            {
					            	filetext+=c.getString(1)+","+c.getString(2)+","+c.getString(3)+","+c.getString(4)+","+c.getString(5)+
					            			","+c.getString(6)+","+c.getString(7)+","+c.getString(8)+","+c.getString(9)+","+c.getString(10)+","+c.getString(11)+","+c.getString(12)+
					            			","+c.getString(13)+","+c.getString(14)+","+c.getString(15)+","+c.getString(16)+","+c.getString(17)+","+c.getString(18)+","+c.getString(19)+
					            			","+c.getString(20)+","+c.getString(21)+",\r\n";
					                //moving cursor to next record
					                c.moveToNext();
					            }
					            //closing cursor
					            c.close();
							
					            fWriter.write(filetext.toString());
							     fWriter.flush();
							     fWriter.close();
							Toast.makeText(getApplicationContext(), "File Exported Successfully", 20000).show();
						} catch (Exception e) {
							Toast.makeText(getBaseContext(), e.getMessage(),
									Toast.LENGTH_SHORT).show();
						}
					break;
				case R.id.btnGoBackExportTables:
					Intent ourGoBackIntent=new Intent(Export.this,MenuPage.class);
					startActivity(ourGoBackIntent);
					break;
			}
		  
	  }
}
